#include "amever.h"

/* when the */ 

struct world_mob *mob_init(const char *name, struct world_cell *cell, int id, int subid)
{
	struct world_mob *me = malloc(sizeof(struct world_mob));
	memset(me, 0, sizeof(struct world_mob));
	me->id = id;
	me->cell = cell;
	me->position = map_addObject(cell, 0, WO_MOB, me);
	strcpy(me->name, name);
	me->status.dead = 0;
	me->status.lazy = 1;
	me->status.hp = 100;
	me->status.sp = 100;
	me->stats.wspeed = 25;
	me->objects = chain_setInit(0,0,5);
	chain_nodeInit(me->objects, (void *)(rand()%1000), 0, 0);
	chain_nodeInit(me->objects, (void *)(rand()%255), 1, 0);
	chain_nodeInit(me->objects, (void *)(rand()%255), 2, 0);	
	chain_nodeInit(me->cell->wmap->mobs, me, me->id, 0);
//	mob_do(me);
	return me;
}

void mob_shut(struct world_mob *me)
{
	if (me->objects)
		chain_setShut(me->objects);
	if(me->steps)
		chain_setShut(me->steps);
	free(me);
}

int mob_sees_char (struct world_mob *mob, struct world_char *wchar) {
	if (mob->target == 0 && DISTANCEC(mob->cell, wchar->cell) <= 9) {
		mob->target = wchar;
		return 1;
	}
	return 0;
}


int mob_dies(struct world_mob *me)
{
	amelog("Mob dies.\n");
	if (me->status.dead == 1)
		return 0;
	me->status.dead = 1;
	struct chain_set *tmp = chain_setInit(0,0,255);
	if (map_foreach(WO_CHAR, me->cell, 9, tmp))
	{
		struct chain_node *noderator = tmp->first;
		struct world_char *c;
		do {
			c = noderator->val;
			WFIFOPR(c->client, 5);
			WFIFOW(c->client, 0) = 110;
			WFIFOW(c->client, 2) = me->id;
			WFIFOW(c->client, 4) = 255;
			WFIFOSP(c->client, 5);
		} while((noderator = noderator->next));
	}
	chain_setShut(tmp);
	return 0;
}
/*
int mob_changeStatus(struct world_mob *me)
{
	struct chain_set *tmp = chain_setInit(0,0,255);
	if (map_foreach(WO_CHAR, me->cell, 9, tmp))
	{
		struct chain_node *noderator = tmp->first;
		struct world_char *c;
		do {
			c = noderator->val;
			WFIFOPR(c->client, 5);
			WFIFOW(c->client, 0) = 110;
			WFIFOW(c->client, 2) = me->id;
			WFIFOW(c->client, 4) = 255;
			WFIFOSP(c->client, 5);
		} while(noderator = noderator->next);
	}
	chain_setShut(tmp);
}
*/

int mob_do (struct world_mob *me) {
	if (me->status.dead) // I'm so dead.
		return 0;
	static char phrases[][64] = {
			"I need to tell you something.",
			"Hi there, my name is %s, please make me a WHOIS.",
			"Yo, let's rumble!",
			"I'm MVP you know?",
			"I'll own you with my [item_link:2:Sword of paper]",
			"How is it going?",
			"Rolling around!",
			"Selling [item_link:1:Poo Hat] (almost new)."
	};
	if (!(rand()%2))
		mob_random_walk(me);
	if (!rand()%3)
	{
		sprintf(s.buffy, phrases[rand() % 8], me->name);  
		mob_chats(me, 2, s.buffy);
	}
	return 0;
}

void mob_chats(struct world_mob *me, int code, const char *phrase)
{
	int size = strlen(phrase) + 5; 
	struct chain_set *receivers = chain_setInit(0, 0, 255);
	if (map_foreach (WO_CHAR, me->cell, 8, receivers) > 0)  {
		struct chain_node *noderator = receivers->first;
		struct client *c;
		while (noderator) {
			c = ((struct world_char *)noderator->val)->client;
			WFIFOPR(c, size);
			WFIFOW(c, 0) = code;
			WFIFOB(c, 2) = size;
			WFIFOW(c, 3) = me->id;
			memcpy(WFIFOP(c, 5), phrase, size-5);
			WFIFOSP(c, size);
			noderator = noderator->next;
		}
	}
	else {
		amelog("Nobody's near\n");
	}
}

int mob_setPosition(struct world_mob *me, struct world_cell *cell)
{
	me->position = map_addObject(cell, me->position, WO_MOB, me);
	struct chain_set *tmp = chain_setInit(0,0,255);
	if (map_foreach(WO_CHAR, cell, CHAR_RADIUS, tmp)) {
		struct chain_node *node = tmp->first;
		do {
			WFIFOPR(CHAR(node->val)->client, 8);
			WFIFOW(CHAR(node->val)->client, 0) = 21;
			WFIFOW(CHAR(node->val)->client, 2) = me->id;
			WFIFOW(CHAR(node->val)->client, 4) = cell->X;
			WFIFOW(CHAR(node->val)->client, 6) = cell->Y;
			WFIFOSP(CHAR(node->val)->client, 8);
		} while ((node = node->next));
	}
	chain_setShut(tmp);
	return 0;
}

int mob_random_walk(struct world_mob *me) {
	struct world_cell* cell = CELL(me->cell);
	int x = cell->X + ((rand()%3) - 1);
	int y = cell->X + ((rand()%3) - 1);
	struct world_cell *newCell = map_getCell(cell->wmap, x, y, 1);
	if (newCell)
	{
		mob_setPosition(me, newCell);
	}
	return 0;
}

int mob_attacked(struct world_mob *me, struct world_char *c)
{
	me->target = c; 
	me->status.attacking = 1;
	if (me->status.hp <= 25)
	{
		mob_chats(me, 2, "Ok, I'm dead now.");
		mob_dies(me);
		return 1;
	}
	else
	{
		mob_getsDamage(me, 25, c);
		return 1;
	}
	return 0;
}

int mob_getsDamage(struct world_mob *me, int damage, struct world_char *c)
{
	me->status.hp -= damage;
	struct chain_set *tmp = chain_setInit(0,0,255);
	if (map_foreach(WO_CHAR,me->cell,9,tmp))
	{
		struct chain_node *noderator = tmp->first;
		struct client *c;
		do {
			c = ((struct world_char *)noderator->val)->client;
			WFIFOPR(c, 8);
			WFIFOW(c, 0) = 111;
			WFIFOW(c, 2) = c->achar->id;
			WFIFOW(c, 4) = me->id;
			WFIFOW(c, 6) = damage;
			WFIFOSP(c, 8);
		} while((noderator = noderator->next));
	}
	chain_setShut(tmp);
	return 0;
}

int mob_getStatus(struct world_mob *me)
{
	if (me->status.dead)
		return 255;
	return 1;
}

/*
int mob_trytogetto(struct world_mob *mob, struct world_cell *cell) {
	if (!mob || !*mob)
		return 0;
	if (DISTANCEP(mob->cell, cell) > mob->status.view_radius)
		return 0;
}
*/
